﻿Imports System.Data.SqlClient

Public Class frmCatalogoSubserviciosDetalle
    Dim servicio As New clsSubServicios
    Dim Bitacora As New clsActividad
    Dim Usuario As New clsUsuarioPrincipal
    Private mstrCondicion As String
    Public WriteOnly Property Condicion() As String
        Set(ByVal value As String)
            mstrCondicion = value
        End Set
    End Property

    Private Sub frmCatalogoSubserviciosDetalle_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        Bitacora.RegistraActividad("Cerró al catálogo detallado de Sub-Servicios")
        Usuario.Ubicacion("Catálogo de Sub-Servicios")
    End Sub
    Private Sub frmCatalogoSubserviciosDetalle_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        ToolStrip1.Cursor = Cursors.Hand
        ToolStrip1.RenderMode = System.Windows.Forms.ToolStripRenderMode.System

        If SubServicioNuevo = True Then
            txtNombre.Text = ""
        Else
            txtNombre.Text = SubServicioSelect
        End If


        'Actualizar()
        CargaSubServicios()

        Bitacora.RegistraActividad("Ingresó al catálogo detallado de Sub-Servicios")
        Usuario.Ubicacion("Catálogo detallado de Sub-Servicios")
    End Sub
    Sub Actualizar()
        Dim strNombre As String = ""
        If txtNombre.Text <> "" Then
            strNombre = Me.txtNombre.Text
            servicio.Nombre = strNombre
        Else
            strNombre = ""
            servicio.Nombre = strNombre
        End If
        servicio.Buscar()
        Scatter()
    End Sub
    Private Sub Scatter()
        txtCosto.Text = Format(servicio.Costo, "###,###.#0")
    End Sub
    Sub CargaSubServicios()
        Windows.Forms.Cursor.Current = Cursors.WaitCursor
        dgBuscarSubservicio.Rows.Clear()

        Dim Filtro As String
        Filtro = UCase(txtBuscarSubServicio.Text)
        Filtro = "%" & Filtro.Replace(" ", "%") & "%"

        Dim cnConn As New SqlConnection
        Dim strSql As String
        Dim cmdBuscar As New SqlCommand
        Dim rdBuscar As SqlDataReader

        Try
            cnConn.ConnectionString = CitraConnection
            strSql = "Select * from SubServicios WHERE UPPER(Nombre) LIKE '" & Filtro & "' " & mstrCondicion

            cmdBuscar.Connection = cnConn
            cmdBuscar.CommandText = strSql


            cnConn.Open()
            rdBuscar = cmdBuscar.ExecuteReader

            Do While rdBuscar.Read()
                dgBuscarSubservicio.Rows.Add(rdBuscar("Nombre").ToString.Trim)

            Loop
        Catch ex As Exception
            MessageBox.Show(ex.Message, "System Message", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End Try
    End Sub

    Private Sub txtBuscarSubServicio_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs)
        If e.KeyChar = Chr(Keys.Enter) Then
            SeleccionarServicio()
        ElseIf e.KeyChar = Chr(Keys.Escape) Then
            panelSubServicios.Visible = False
            txtNombre.SelectAll()
            txtNombre.Focus()

        End If
    End Sub

    Private Sub txtBuscarSubServicio_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtBuscarSubServicio.TextChanged
        CargaSubServicios()
    End Sub
    Private Sub dgBuscarRuta_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgBuscarSubservicio.DoubleClick
        SeleccionarServicio()
    End Sub

    Private Sub dgBuscarRuta_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles dgBuscarSubservicio.KeyPress
        SeleccionarServicio()
    End Sub
    Sub SeleccionarServicio()
        Dim valorCell As Integer
        Dim valorRow As DataGridViewRow

        valorCell = dgBuscarSubservicio.CurrentCell.ColumnIndex
        valorRow = dgBuscarSubservicio.CurrentRow

        Dim Servicio As String = dgBuscarSubservicio.Rows(valorRow.Index).Cells(0).Value.ToString.Trim
        txtNombre.Text = Servicio

        panelSubServicios.Visible = False
        txtNombre.SelectAll()
        txtNombre.Focus()
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        panelSubServicios.Visible = True
        txtBuscarSubServicio.Clear()
        txtBuscarSubServicio.Focus()

    End Sub
    Private Sub Gatter()
        servicio.Nombre = txtNombre.Text.Trim
        servicio.Costo = txtCosto.Text
    End Sub

    Private Sub ToolStripButton2_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripButton2.Click
        Guardar()


    End Sub
    Sub Guardar()
        Gatter()
        Dim IdServicio As Integer = BuscarIntDatoCitra("SELECT IdSubServicio FROM SubServicios WHERE Nombre='" & txtNombre.Text.Trim & "'")
        If IdServicio = 0 Then
            servicio.Insertar()
        Else
            servicio.Actualizar()
        End If
        txtNombre.Clear()
        txtNombre.Focus()
        frmSubServicios.CargaSubservicios()
    End Sub

    Private Sub GuardarToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles GuardarToolStripMenuItem.Click
        Guardar()

    End Sub

    Private Sub RegresarToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles RegresarToolStripMenuItem.Click
        Close()

    End Sub

    Private Sub ToolStripButton1_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripButton1.Click
        Close()

    End Sub

    Private Sub NuevaToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles NuevaToolStripMenuItem.Click
        txtNombre.Clear()
        txtNombre.Focus()

    End Sub

    Private Sub txtNombre_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtNombre.TextChanged
        Actualizar()
    End Sub
End Class